/* ************************************************************************

   Copyright:	

   License:	

   Authors:	TPham

************************************************************************ */

/* ************************************************************************

#asset(rocketspocket/*)

************************************************************************ */

/**
 * This is the main application class of the application "RocketsPocket"
 */
qx.Class.define("rocketspocket.Application",
{
	extend : qx.application.Standalone,

	/*
	*****************************************************************************
	 MEMBERS
	*****************************************************************************
	*/

	members :
	{
		/**
		 * Properties
		 */
		mainPanel : null,
		/**
		 * This method contains the initial application code and gets called 
		 * during startup of the application
		 * 
		 * @lint ignoreDeprecated(alert)
		 */
		main : function()
		{
			// Call super class
			this.base(arguments);
			// Enable logging in debug variant
			if (qx.core.Environment.get("qx.debug"))
			{
				// support native logging capabilities, e.g. Firebug for Firefox
				qx.log.appender.Native;
				// support additional cross-browser console. Press F7 to toggle visibility
				qx.log.appender.Console;
			}
			/*-------------------------------------------------------------------------
			Actual application code...
			-------------------------------------------------------------------------*/
			var scroller = new qx.ui.container.Scroll();
			var headerPanel = new qx.ui.container.Composite(new qx.ui.layout.HBox());
			var menuPanel = new qx.ui.container.Composite(new qx.ui.layout.Grid(3, 3));
			menuPanel.set({
				padding : 20	 
			});
			var btnYahoo = new qx.ui.form.Button(this.tr("Show Yahoo"));
			btnYahoo.addListener("execute", function() {
				var yahoo = new rocketspocket.ui.yahoo.YahooWindow();
				yahoo.open();
			}, this);
			
			var btnTwitter = new qx.ui.form.Button(this.tr("Show Twitter"));
			btnTwitter.addListener("execute", function() {
				this.__openTwitter();
			}, this);
			
			var btnTestRpc = new qx.ui.form.Button(this.tr("Test RPC"));
			btnTestRpc.addListener("execute", function() {
				this.__testRPC();
			}, this);
			
			var btnMiniBrowser = new qx.ui.form.Button(this.tr("Mini Browser"));
			btnMiniBrowser.addListener("execute", function() {
				this.__openMiniBrowser();
			}, this);
			
			var btnSumoPaint = new qx.ui.form.Button(this.tr("Sumo Paint"));
			btnSumoPaint.addListener("execute", function() {
				this.__openNewPage("http://www.sumopaint.com/app/api.php?key=ecbgbdzqttnspghs", 970, 700);
			}, this);
			
			var btnSolitare = new qx.ui.form.Button(this.tr("Solitare"));
			btnSolitare.addListener("execute", function() {
				this.__openNewPage("http://pasjans-online.pl/", 970, 700);
			}, this);
			
			var settingsWindow = null;
			var btnPreferences = new qx.ui.form.Button(this.tr("Preferences"));
			btnPreferences.addListener("execute", function() {
				if (!settingsWindow) {
					settingsWindow = new rocketspocket.ui.system.SettingsWindow();
					settingsWindow.moveTo(200,20);
				}
				settingsWindow.open();
			}, this);
			
			var btnHelp = new qx.ui.form.Button(this.tr("Help"));
			btnHelp.addListener("execute", function() {
				this.__openNewPage("https://developers.google.com/appengine/", 970, 700);
			}, this);
			
			var btnAbout = new qx.ui.form.Button(this.tr("About"));
			btnAbout.addListener("execute", function() {
				alert("\nProject name: RocketsPocket" +
					"\nA rich internet application for relaxing." +
					"\nDeveloped basing on Qooxdoo Framework." +
					"\nWeb technologies in use: HTML5, CSS3, Jquery, 960 Grid System, Blueprint Framework." +
					"\nServer script: Java Servlet." +
					"\nInformation exchange: JSON, JSONP, JSON-RPC." +
					"\nPowered by Google App Engine, Google Code." +
					"\nOwner: 7inpham@gmail.com");
			}, this);
			
			menuPanel.add(btnYahoo, {row : 0, column : 0});
			menuPanel.add(btnTwitter, {row : 1, column : 0});
			menuPanel.add(btnTestRpc, {row : 2, column : 0});
			menuPanel.add(btnMiniBrowser, {row : 3, column : 0});
			menuPanel.add(btnSumoPaint, {row : 4, column : 0});
			menuPanel.add(btnSolitare, {row : 5, column : 0});
			menuPanel.add(btnPreferences, {row : 6, column : 0});
			menuPanel.add(btnHelp, {row : 7, column : 0});
			menuPanel.add(btnAbout, {row : 8, column : 0});
			
			this.mainPanel = new qx.ui.container.Composite(new qx.ui.layout.Grow());
			this.mainPanel.add(headerPanel);
			this.mainPanel.add(menuPanel);
			scroller.add(this.mainPanel);
			this.getRoot().add(scroller, {edge : 0});
		},
		
		__openTwitter : function(){
			var main = new rocketspocket.ui.twitter.MainWindow();
			// main.setModal(true);
			main.moveTo(200, 20);
			main.open();
			
			var service = new rocketspocket.ui.twitter.TwitterService();
			//service.addListener("changeTweets", function(e) {
				//this.debug(qx.dev.Debug.debugProperties(e.getData()));
			//}, this);
			
			// handler after posting a tweet
			service.addListener("postOk", function() {
				main.clearPostMessage();
				service.fetchTweets();
			}, this);
			
			// reload handling
			main.addListener("reload", function() {
				service.fetchTweets();
			}, this);
			
			// post handling
			main.addListener("post", function(e) {
				var msg = e.getData();
				if (!this.__loginWindow) {
					this.__loginWindow = new rocketspocket.ui.twitter.LoginWindow();
					this.__loginWindow.addListener("changeLoginData", function(ev) {
						var loginData = ev.getData();
						service.fetchTweets(msg, loginData.username, loginData.password);
					});
					this.__loginWindow.moveTo(720, 160);
					this.__loginWindow.open();
				} else {
					this.__loginWindow.open();
				}
			}, this);
			
			
			// create the controller
			var list = main.getList();
			list.setItemHeight(68);
			list.setDelegate({
				createItem : function() {
					return new rocketspocket.ui.twitter.TweetView();
				},
				
				bindItem : function(controller, item, id) {
					controller.bindProperty("text", "post", null, item, id);
					controller.bindProperty("user.profile_image_url", "icon", null, item, id);
					controller.bindProperty("created_at", "time", {
						converter: function(data) {
							if (qx.core.Environment.get("engine.name")) {
								data = Date.parse(data.replace(/( \+)/, " UTC$1"));
							}
							return new Date(data);
						}
					}, item, id);
				},
				
				configureItem : function(item) {
					item.getChildControl("icon").setWidth(48);
					item.getChildControl("icon").setHeight(48);
					item.getChildControl("icon").setScale(true);
					item.setMinHeight(52);
				}
			});
			
			service.bind("tweets", list, "model", {
				converter : function(value) {
					return value || new qx.data.Array();
				}
			});
			
			// start the loading on startup
			service.fetchTweets();
			
			// set language
			// qx.locale.Manager.getInstance().setLocale("vi");
		},
		
		__testRPC : function(){
   			var rpc = new qx.io.remote.Rpc();	
			rpc.setCrossDomain(false);
//			rpc.setTimeout(1000);
			
			var host = window.location.host;
			var proto = window.location.protocol;
			var webURL = proto + "//" + host + "/rsprpc";
			rpc.setUrl(webURL);
			rpc.setServiceName("qooxdoo.test");
			var handler = function(result, ex, id){
  				if (ex == null) {
  	       			alert(result);
      			} else {
    				alert("Async(" + id + ") exception: " + ex);
  				}
			}
			rpc.callAsync(handler, "echo", "Hello to qooxdoo World!");
//			rpc.callSync("qooxdoo.test", "HELLO");
		},
		
		__openMiniBrowser : function(){
			var wnd = new qx.ui.window.Window();
			var layout = new qx.ui.layout.Grid(0, 0);
			layout.setRowFlex(1, 1);
			layout.setColumnFlex(0, 1);
			wnd.setLayout(layout);
			wnd.setHeight(325);
			wnd.setWidth(430);
			wnd.moveTo(200, 20);
			var label = new qx.ui.basic.Label("Source: ");
			var txtSrc = new qx.ui.form.TextField();
			txtSrc.setPlaceholder("Type your source...");
			var goBtn = new qx.ui.form.Button("Go");
			goBtn.addListener("execute", function(e){
				frame.setSource("http://"+txtSrc.getValue());
			});
			
			var frame = new qx.ui.embed.ThemedIframe();
			frame.addListener("load", function(e) {
				this.debug("Loaded: " + this.getSource());
			});
			frame.setHeight(315);
			frame.setWidth(420);
			frame.setSource("http://www.youtube.com/embed/JZ42p40qDEc");
			wnd.add(label, {row : 0, column : 0});
			wnd.add(txtSrc, {row : 0, column : 1});
			wnd.add(goBtn, {row : 0, column : 2});
			wnd.add(frame, {row : 1, column : 0, colSpan : 6});
			wnd.open();
		},
		
		__openNewPage : function(src, width, height){
			var wnd = new qx.ui.window.Window();
			wnd.setShowMinimize(false);
			wnd.moveTo(200, 20);
			var layout = new qx.ui.layout.Grow();
			wnd.setLayout(layout);
			wnd.setHeight(height);
			wnd.setWidth(width);
			// Iframe
			var frame = new qx.ui.embed.ThemedIframe();
			frame.addListener("load", function(e) {
				this.debug("Loaded: " + this.getSource());
			});
			frame.setHeight(height);
			frame.setWidth(width);
			frame.setSource(src);
			wnd.add(frame);
			wnd.open();
		}
	} // end members
});	// end class
